<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>购物车 - {wc:fun:_cfg("web_name")}</title>
<meta name="keywords" content="{wc:if isset($keywords)}{wc:$keywords}{wc:else}{wc:fun:_cfg("web_key")}{wc:if:end}" />
<meta name="description" content="{wc:if isset($description)}{wc:$description}{wc:else}{wc:fun:_cfg("web_des")}{wc:if:end}" />
<link rel="stylesheet" type="text/css" href="{G_TEMPLATES_STYLE}/css/Comm.css"/>
<link rel="stylesheet" type="text/css" href="{G_TEMPLATES_STYLE}/css/CartList.css"/>
<script type="text/javascript" src="{G_GLOBAL_STYLE}/global/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="{G_TEMPLATES_STYLE}/js/jquery.cookie.js"></script>
<style type="text/css">
	.yj-mask{width: 100%;height: 100%;background: black;position: absolute;top: 0px;z-index: 99;opacity: 0.7;}
	.yj-content{width: 400px;height:auto;max-height:700px;background: white;position: absolute;top: 13%;left: 34%;z-index: 100;overflow-y: auto;}
	.yj-topbar{width:100%;height:35px;background: #f8f8f8;border-top: 1px solid #dfdfdf;border-bottom 1px solid #efefef;border-bottom: 1px solid #dfdfdf;position: sticky;top: 0px;text-align: center;line-height: 35px;}
	.yj-content table{width: 100%;}
	.yj-content table td{height: 35px}
	.yj-allrand{position: absolute;right: 50px;cursor: pointer;}
	.yj-button{padding: 5px 7px;background: red;color: white;border-radius: 3px;cursor: pointer;}
	.yj-bottombar{text-align: center;bottom: 0px;padding-top: 10px;padding-bottom: 10px;position: sticky;background: #f8f8f8;border-top: 1px solid #dfdfdf; border-bottom: 1px solid #dfdfdf;}
	.yj-xj{width: 130px;display: block;float: left;line-height: 30px;text-align: center;}
	.yj-xjs{width: 130px;display: block;float: left;height: 60px;word-break: normal;word-wrap: break-word;padding-top: 30px;text-align: center;overflow-y: auto;}
	.yj-random{text-align: center;}
	.ygcode{text-align: center;}
	.single_rand{cursor: pointer;}

</style>
</head>
<body>
<div class="logo">
	<div class="float">
		<span class="logo_pic"><a href="{G_WEB_PATH}" class="a" title="{wc:fun:_cfg("web_name")}">
			<img src="{G_UPLOAD_PATH}/{wc:fun:Getlogo()}"/>
		</a></span>
		<span class="tel"><a href="{G_WEB_PATH}" style="color:#999;">返回首页</a></span>
	</div>
</div>
<div class="shop_process">
	<ul class="process">
		<li class="first_step">第一步：	订单</li>
		<li class="arrow_1"></li>
		<li class="secend_step">第二步：网银支付</li>
		<li class="arrow_2"></li>
		<li class="third_step">第三步：支付成功 等待揭晓</li>
		<li class="arrow_2"></li>
		<li class="fourth_step">第四步：揭晓获得者</li>
		<!-- <li class="arrow_2"></li>
		<li class="fifth_step">第五步：晒单奖励</li> -->
	</ul>
	<div class="i_tips"></div>
	<div class="submitted">
		<ul class="order">
			<li class="top">
				<span class="goods">商品</span>
				<span class="name">名称</span>
				<span class="moneys">价值</span>
				<span class="money">{wc:fun:_cfg('web_name_two')}价</span>
				<span class="num">{wc:fun:_cfg('web_name_two')}人次</span>
				<span class="xj">小计</span>
				<span class="moneys">云购码</span>
				<span class="do">操作</span>
			</li>
			{wc:loop $shoplist $shops}            
			<li class="end" id="shoplist{wc:$shops['id']}" style="width: 1199px;">
				<span class="goods">
					<a href="{WEB_PATH}/goods/{wc:$shops['id']}">
                   	 <img src="{G_UPLOAD_PATH}/{wc:$shops['thumb']}"/>
                    </a>                    
				</span>
				<span class="name">
					<a href="{WEB_PATH}/go/index/item/{wc:$shops['id']}" >{wc:$shops['title']}</a>
					<p>总需 <span class="color">{wc:$shops['zongrenshu']}</span>人次参与，还剩 
                      	   <span class="gorenci">{wc:$shops['cart_shenyu']}</span> 人次
                    </p>
				</span>
				<span class="moneys">￥{wc:$shops['money']}</span>
				<span class="money"><span class="color">￥<b>{wc:$shops['yunjiage']}</b></span></span>
				<span class="num">				
					<dl class="add">					
					<dd><input type="type" attr_limit_buy="{wc:$shops['limit_buy']}" attr_limit_buys="{wc:$shops['limit_buys']}"  val="{wc:$shops['id']}" onkeyup="value=value.replace(/\D/g,'')" value="{wc:$shops['cart_gorenci']}" class="amount" /></dd>
						<dd><!-- wk attr_id="{wc:$shops['attr_limit_buy']}"  attr_limit_buys="{wc:$shops['limit_buys']}" -->
							<a href="JavaScript:;" attr_limit_buy="{wc:$shops['limit_buy']}" attr_limit_buys="{wc:$shops['limit_buys']}"  val="{wc:$shops['id']}" class="jia"></a>
							<a href="JavaScript:;" attr_limit_buy="{wc:$shops['limit_buy']}" attr_limit_buys="{wc:$shops['limit_buys']}" val="{wc:$shops['id']}" class="jian"></a>
						</dd>                        
					</dl>
                    <p class="message"></p>
				</span>
				<span  class="xj">{wc:$shops['cart_xiaoji']}</span>
				<div style="padding-top: 15px;width: 130px;float: left;" id="d{wc:$shops['id']}" class="yj-d">
					<span class="yj-xj" id="m{wc:$shops['id']}" style="cursor: pointer;" onclick="input_yg_code(this,0);">机选云购码</span>
					<span class="yj-xj" id="u{wc:$shops['id']}" style="cursor: pointer;" onclick="input_yg_code(this,1);">输入云购码</span>
				</div>
				<div class="yj-xjs" id="s{wc:$shops['id']}" style="display: none;"></div>
				<span class="do">
					<a href="JavaScript:;" onclick="re_sel({wc:$shops['id']})">重选</a>
					<a href="javascript:;" onclick="delcart({wc:$shops['id']})"  class="delgood">删除</a>
				</span> 
			</li>		
			{wc:loop:end}
			<li class="ts">
				<p class="right">{wc:fun:_cfg('web_name_two')}金额总计:￥<span id="moenyCount">{wc:$MoenyCount}</span></p>
			</li>
		</ul>
	</div>
	<h5>
		<a href="{WEB_PATH}" id="but_on"></a>
		<input id="but_ok" type="button" value=""  name="submit"/>
	</h5>
</div> 

<div class="yj-mask" style="display: none;" onclick="hide_mask();">
</div>
<div class="yj-content" style="display: none;">
	<div class="yj-topbar">输入云购码<span class="yj-allrand" onclick="all_rand()">全部机选</span></div>
	<table width="100%"  id="yj-table">
	</table>
	<div class="yj-bottombar" style="text-align: center;">
		<span class="yj-button" onclick="code_ok()">确定</span>
		<span class="yj-button" onclick="all_clear()">重置</span>
		<span class="yj-button" onclick="hide_mask()">关闭</span>
	</div>
</div> 
<script type="text/javascript"> 
var info={wc:$Cartshopinfo};
var numberadd=$(".jia");
var numbersub=$(".jian");
var xiaoji=$(".xj");
var num=$(".amount");
var message=$(".message");
var moenyCount=$("#moenyCount");


$(function(){
	$("#but_ok").click(function(){
		var is_ok = true
		if (is_ok) {
			var countmoney=parseInt(moenyCount.text());
			var err = check_ygcode();
			if (err == true) {
				if(countmoney > 0){		
					//$.cookie('Cartlist','',{path:'/'});
					 $.each(info, function(i){     
					   info[i]['temp_list'] = '' ;    
					   info[i]['ygcode_list'] = '';     
					});
					$.cookie('Cartlist',$.toJSON(info),{expires:7,path:'/'});
					document.location.href='{WEB_PATH}/member/cart/pay/'+new Date().getTime();
				}else{
					alert("购物车为空!");
				}
			}
		}
	});
});

function UpdataMoney(shopid,number,zindex){		
	var number = parseInt(number);
	info['MoenyCount']=info['MoenyCount']-info[shopid]['money']*info[shopid]['num']+info[shopid]['money']*number;
	info[shopid]['num']=number;
	var xjmoney=xiaoji.eq(zindex+1);
		xjmoney.text(info[shopid]['money']*number+'.00');
		moenyCount.text(info['MoenyCount']+'.00');
}


function delcart(id){
	info['MoenyCount'] = info['MoenyCount']-info[id]['money']*info[id]['num'];
	$("#shoplist"+id).remove();
	$("#moenyCount").text(info['MoenyCount']+".00");
	delete info[id];
	//$.cookie('Cartlist','',{path:'/'});
	$.cookie('Cartlist',$.toJSON(info),{expires:30,path:'/'});
}

num.keyup(function(){
	var shopid=$(this).attr("val");
	var zindex=num.index(this);
	//修改数量清空云购码		
	re_sel(shopid);	
	if($(this).val() > info[shopid]['shenyu']){
		message.eq(zindex).text("购买次数不能超过"+info[shopid]['shenyu']+"次");		
		$(this).val(info[shopid]['shenyu']);
		UpdataMoney(shopid,$(this).val(),zindex);		
		return;
	}
	if($(this).val()<1){
		message.eq(zindex).text("购买次数不能少于1次");
		$(this).val(1);
		UpdataMoney(shopid,$(this).val(),zindex);
		return;
	}	
	
	/* wk  限购 s */
 
	var limit_buy=$(this).attr('attr_limit_buy');	/* wk 限购  */	
	var limit_buys=$(this).attr('attr_limit_buys');	/* wk 允许限购  */	
	var yi = limit_buys - limit_buy;
	if(limit_buys > 0 && $(this).val() > limit_buy){
		$(this).val(limit_buy);
		message.eq(zindex).text("本商品限购"+limit_buys+"次;已购"+yi+'次');
		return;
	}
	/* wk  限购 e */
	
	UpdataMoney(shopid,$(this).val(),zindex);	
});
numberadd.click(function(){

	var shopid=$(this).attr('val');
	
	var zindex=numberadd.index(this);
	var thisnum=num.eq(zindex);	
		if(info[shopid]['num'] >= info[shopid]['shenyu']){
			message.eq(zindex).text("购买次数不能超过"+info[shopid]['shenyu']+"次");
			return;
		}
		//修改数量清空云购码		
		re_sel(shopid);
		var number=info[shopid]['num']+1;	
		/* wk  限购 s */
		var limit_buy=$(this).attr('attr_limit_buy');	/* wk 限购  */	
		var limit_buys=$(this).attr('attr_limit_buys');	/* wk 允许限购  */	
		var yi = limit_buys - limit_buy;
		if(limit_buys > 0 && number > limit_buy){
			message.eq(zindex).text("本商品限购"+limit_buys+"次;已购"+yi+'次');
			return;
		}
		/* wk  限购 e */
				
			thisnum.val(number);
			UpdataMoney(shopid,number,zindex);
});
numbersub.click(function(){
	var shopid=$(this).attr('val');		
	var zindex=numbersub.index(this);
	var thisnum=num.eq(zindex);	
		if(info[shopid]['num'] <=1){
			message.eq(zindex).text("购买次数不能少于1次");
			return;
		}
		var number=info[shopid]['num']-1;			
			thisnum.val(number);
			UpdataMoney(shopid,number,zindex);
			//修改数量清空云购码		
			re_sel(shopid);
});

//////////////////////////////////////////////////////////////////////
//////选号的相关方法  yjmp
var choice_id = '';
var list_id = '';
var code_list = new Array();
var used = new Array();
//隐藏蒙层  
function hide_mask()
{
	$(".yj-mask").css('display','none');
	$(".yj-content").css('display','none');
	used.splice(0,used.length)
}

//显示云购码输入框 (可用数内)
/**
 *	生成云码输入框
 * @param  obj item 标签实体
 * @param  int type 类型 0为机先，1为手动
 */
function input_yg_code(item,type)
{
	$(".yj-mask").css('display','block');
	$(".yj-content").css('display','block');
	var temp_id = item.id;
	choice_id = temp_id.substr(1,temp_id.length-1);
	list_id = "s"+choice_id;

	reset_ygcode_list();
	var listId = "shoplist"+choice_id;
	var times = $("#"+listId).find(".amount").val();
	$('#yj-table').html("");
	var money = parseInt(info[choice_id]['yjmoney'])
	var placeholder = '输入1-'+money+'之间的任意值';
	/*机选模式*/
	if (type == 0) {
		for (var i = 0; i < times; i++) {
			$('#yj-table').append("<tr><td class='yj-input' align='right' width='60%'><input type='text' name='ygcode' class='ygcode' value='' placeholder='"+placeholder+"' readonly /></td><td class='yj-random'><span class='single_rand' onclick='single_rand(this)' >机选</span></td></tr>");
		}
		$(".yj-allrand").css('display','')
	}else{/*手选模式*/
		for (var i = 0; i < times; i++) {
			$('#yj-table').append("<tr><td class='yj-input' align='center' width='100%'><input type='text' name='ygcode' class='ygcode' value='' placeholder='"+placeholder+"' onkeyup ='check_input(this)'/></td></tr>");
		}
		$(".yj-allrand").css('display','none')
	}
}
/*检查输入内容*/
function check_input(obj){
	var money = parseInt(info[choice_id]['yjmoney'])
	if (obj.value > money) {
		obj.value = ''
		alert('请输入1-'+money+'之间任意值')
	}
}

//单个随机 
function single_rand(item)
{
	var yjkey = 0;
	var count = 1;
	console.log(used);
	$.ajax({
		url:'{WEB_PATH}/member/cart/single_rand',
		type:'POST',
		dataType:'JSON',
		data:{'num':count,'used':used,'shopid':choice_id},
		success:function(e){
			code_list = e.content
			$('.single_rand').each(function(key,object){
				if (object == item) {
					yjkey = key
				}
			});

			$('.ygcode').each(function(key,object){
				if (key == yjkey) {
					//console.log($(object).val())
					var old_val = $.trim($(object).val())
					if (used.length > 0 && old_val.length > 0) {
						for (var i = 0; i < used.length; i++) {
							if (old_val == used[i]) {
								used.splice(i,1)
							}
						}
					}
					$(object).val(code_list[0])
					used.push(code_list[0])
					console.log(used)
				}
			});
		},
		error:function(e){
			alert('网络错误')
		}
	});
}


//全部随机方法  
function all_rand()
{
	all_clear()
	var count = $('.ygcode').length
	console.log(used);
	$.ajax({
		url:'{WEB_PATH}/member/cart/single_rand',
		type:'POST',
		dataType:'JSON',
		data:{'num':count,'used':used,'shopid':choice_id},
		success:function(e){
			console.log(e)
			code_list = e.content
			$('.ygcode').each(function(key,object){
				$(object).val(code_list[key])
				used.push(code_list[key])
			});
		},
		error:function(e){
			alert('网络错误')
		}
	});
}


//检查输入框内是否有重码 重复返回0 不重复返回1
function check_same(){
	var err = 1
	$('.ygcode').each(function(key,object){
		$('.ygcode').each(function(key1,object1){
			if (object.value == object1.value && object1 != object && object.value != '') {
				err = 0	
			}
		});
	});
	return err
}
//检查是否存在不可用验证码(不在序列里的)
function unuseable_msg(){

	var code_arr = info[choice_id]['temp_list'];
	var err = 1;
	var err_msg = '';

	$('.ygcode').each(function(key,object){
		for (var i = 0; i < code_arr.length; i++) {
			if (object.value == code_arr[i] || object.value == '') {
				err = 0;
			}
		}

		if (err) {
			err_msg += object.value
			err_msg += ','
		}
		err = 1;
	});

	if (err_msg != '') {
		err_msg = '云购码：'+err_msg+'格式错误或已被使用'
	}

	return err_msg
}

//全部清空处理 
function all_clear()
{
	$('.ygcode').each(function(key,object){
		$(object).val('')
	});
	used.splice(0,used.length)
}
//确定
function code_ok()
{
	var err = 0
	var err_msg = ''
	var unuseable_err = unuseable_msg() //合法性

	$('.ygcode').each(function(key,object){
		if(object.value== ''){
			err = 1
		}
	});

	if (err == 1) {
		err_msg += "云购码不能为空\n";
	}

	if (unuseable_err != '') {
		err_msg += unuseable_err
	}

	if (err_msg != '') {
		alert(err_msg)
	}else{
		hide_mask()
		var temp_code = get_temp_ygcode()
		$("#d"+choice_id).css('display','none')
		$("#"+list_id).html(temp_code)
		//数据存入info
		info[choice_id].ygcode=temp_code
		$("#"+list_id).css('display','block')
	}
	console.log(info)
}
/**
 * 获取临时云购码
 * @return {string} 逗号隔开的所有去购码
 */
function get_temp_ygcode()
{
	var temp_code = ''
	$('.ygcode').each(function(key,object){
		temp_code += $(object).val()
		temp_code += ','
	});
	
	var length = temp_code.length - 1
	temp_code = temp_code.substr(0,length)
	return temp_code
}
/**
 * 重选
 * @param  {int} val shoplistId
 */
function re_sel(val)
{
	$("#d"+val).css('display','block')
	$("#s"+val).css('display','none')
	$("#s"+val).html()
	list_id = ''
	if (used) {
		used.splice(0,used.length)
	}
}

/**
 * 检查云购码输入
 */
function check_ygcode()
{
	var err = 0

	$('.yj-d').each(function(key,object){
		if ($(object).css('display') == 'block') {
			err = 1
		}
	});
	if (err) {
		alert('云购码没有输完')
		return false
	}else{
		return true
	}
}
/**
 * 返回选择商品的可用云购码数组
 * @param  商品id shopid 商品id
 * @return {array}   可用去购码数组
 */
function get_usable_ygcode(shopid){
	var id_arr = info[shopid]['ygcode_list'].split(',')
	return id_arr
}

/**
 * 重置临时数据云购码序列
 */
function reset_ygcode_list(){

	var id_arr = get_usable_ygcode(choice_id)
	info[choice_id]['temp_list'] = id_arr
}
</script>
<!--footer 开始-->
{wc:templates "index","footer"}